package com.hongdee.crm.base.service.impl;

import com.hongdee.base.Constant;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.base.dao.ICarDao;
import com.hongdee.crm.base.entity.TCrmSurveryItem;
import com.hongdee.crm.base.entity.TCrmSurveyGroup;
import com.hongdee.crm.base.service.ITCrmBaseItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dell on 2017/5/10.
 */
@Service
public class TCrmBaseItemServiceImpl implements ITCrmBaseItemService {
    @Autowired
    private ICarDao iCarDao;
    private String tabName = "T_CRM_SURVERY_ITEM";
    @Autowired
    private IHibernateDao iHibernateDao;

    @Override
    public void update(String id, String status) throws Exception {
        String hql = "update TCrmSurveryItem  set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.iCarDao.update(hql, params);
    }

    @Override
    public TCrmSurveryItem saveOrUpdate(String uid,TCrmSurveryItem tCrmSurveyItem ) throws Exception {
        System.out.println();
        tCrmSurveyItem.setCompanyId(TokenUtil.getCompanyId(uid));
        //根据问题编号、问题名称去重
        if (null == tCrmSurveyItem.getId() || StringUtils.isEmpty(tCrmSurveyItem.getId())) {
            //新增时判断是否已经存在
            if (isExist(tCrmSurveyItem.getItemCode(), tCrmSurveyItem.getItemName(),tCrmSurveyItem.getGroupCode(), null,0)) {

                throw new Exception("问题编号或问题名称名称已存在");
            }
            tCrmSurveyItem.setStatus("1");
            tCrmSurveyItem.setId(null);
            this.iCarDao.saveOrUpdate(tCrmSurveyItem);
        } else {
            if (isExist(tCrmSurveyItem.getItemCode(), tCrmSurveyItem.getItemName(),tCrmSurveyItem.getGroupCode(),tCrmSurveyItem.getId(),1) ){
                throw new Exception("问题编号或问题名称名称已存在");
            }
            this.iCarDao.saveOrUpdate(tCrmSurveyItem);
        }
        //修正下一级编码
        System.out.println(tCrmSurveyItem);
        return tCrmSurveyItem;
    }
    //判断问题编号，问题名称是否重复
    private boolean isExist(String itemCode, String itemName, String groupCode,String id,int type) throws Exception {
        String sql = "select * from " + tabName + " where group_code = ? and ( item_code= ? or item_name = ? ) ";
        List param = new ArrayList();
        param.add(groupCode);
        param.add(itemCode);
        param.add(itemName);
        if ( 1==type) {
            sql += " and  id != ? ";
            param.add(id);
        }
        Map map = this.iCarDao.page(sql, param.toArray(), "0", "50");
        return Integer.valueOf(map.get("total").toString()) > 0;
    }
    /*
    * 根据条件查询
    * */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select a.*,b.group_name groupName from " + tabName + " a,t_crm_survery_group b where a.id = ? and a.group_code = b.group_code ";
        Map map = this.iCarDao.info(sql, id);

        return map;
    }

    /*
    * 分页
    * **/
    @Override
    public Map newPage(String start, String limit, String id,String itemCode,String groupCode) throws Exception {
        String sql = "select a.*,b.group_name groupName from T_CRM_SURVERY_ITEM a left join T_CRM_SURVERY_GROUP b on a.group_code = b.group_code  where 1=1 ";

         List<Object> params = new ArrayList();

        if (!StringUtils.isEmpty(groupCode)) {
            sql += "and a.group_code = ? ";
            params.add(groupCode);
            }

        if (!StringUtils.isEmpty(itemCode)) {
            //if (!StringUtils.isEmpty(id)) {
             sql += " and (a.item_code like ? or a.item_name like ? )";

            params.add("%" + itemCode + "%");
            params.add("%" + itemCode + "%");
            }
        return this.iCarDao.page(sql, params.toArray(), start, limit);
    }
 /*
 * 左侧树方法
 * */
    @Override
    public List tree(String uid) throws Exception {
        //sql查询
        String sql = "select * from T_CRM_SURVERY_GROUP where status = '1' ";
        return this.iCarDao.list(sql);

    }
}
